#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"

static void PrintGrayCode(int nBits);
static void normalBit(string bitString, int stringLen);
static void reversedBit(string bitString, int stringLen);

int main() {
	int nBits;
	printf("-- Assignment 1 --\n\n");
	printf("-- GrayCode --\n\n");
	printf("Enter number of bits: ");
	nBits = GetInteger();
	
	printf("\n");
	PrintGrayCode(nBits);
	printf("\n");
	system("pause");
}

static void PrintGrayCode(int nBits) {
	string bitString = "";
	normalBit(bitString, nBits);
}

static void normalBit(string bitString, int stringLen) {
	if (stringLen == 0) {
		printf("%s\n", bitString);
		return;
	} else {
		normalBit(Concat(bitString, "0"), stringLen-1);
		reversedBit(Concat(bitString, "1"), stringLen-1);
	}
}

static void reversedBit(string bitString, int stringLen) {
	if (stringLen == 0) {
		printf("%s\n", bitString);
	} else {
		normalBit(Concat(bitString, "1"), stringLen-1);
		reversedBit(Concat(bitString, "0"), stringLen-1);
	}
}